package com.sqly.doc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cazaea.sweetalert.SweetAlertDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.BleEquip;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 蓝牙设备列表
 * Created by Administrator on 2019/12/18 0018.
 */

public class bleequip_list extends MyBaseActivity {
    List<BleEquip> list;//信息列表
    XListView listView;
    ZLoadingDialog dialog;//加载动画
    Context con = bleequip_list.this;
    private int pageNum1 = 1;// 无关键字页数

    //状态栏
    TextView title, uname, count;
    LinearLayout back;

    @Override
    protected void initUI() {
        setContentView(R.layout.bleequip_list);
        listView = (XListView) findViewById(R.id.bleequiplistView);
        listView.setPullLoadEnable(false);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
        //状态栏
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        back = (LinearLayout) findViewById(R.id.back);
        title.setText("蓝牙设备绑定");
        uname.setText("绑定");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(con, bleequip_add.class).putExtra("type", "add"));
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        getBleEquips();
    }

    @Override
    protected void initListener() {

    }

    public void getBleEquips() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        UserClient.get(HttpUrl.GetAllBleEquip, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                dialog.show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                dialog.dismiss();
                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    if (pageNum1 == 1)
                        list = new ArrayList<BleEquip>();
                    List<BleEquip> ll = JSON.parseArray(ret.getRd(), BleEquip.class);
                    JSONArray js = JSON.parseArray(ret.getRd());
                    for (int i = 0; i < js.size(); i++) {
                        JSONObject j = js.getJSONObject(i);
                        ll.get(i).setBid(j.getString("id"));
                    }
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }

                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getBleEquips();
                        }

                        @Override
                        public void onLoadMore() {
                        }
                    });
                    listView.setAdapter(new CommonAdapter<BleEquip>(con, list, R.layout.bleequip_list_item) {
                        @Override
                        public void convert(ViewHolder helper, BleEquip item) {
                            helper.setText(R.id.name, item.getName());
                            helper.setText(R.id.count, item.getMac());
                            helper.setText(R.id.dicount, item.getSn());
                            helper.setText(R.id.category, null != item.getCategory() && "1".equals(item.getCategory())?"血糖仪":"血压仪");
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                            if(SharedPreferencesUtils.getParam(con,"isxzgly","").equals("")){

                            }else {
                                new SweetAlertDialog(con, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("提示")
                                        .setContentText("确定删除该设备?")
                                        .setConfirmText("确定")
                                        .setCancelText("取消")
                                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                            @Override
                                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                                sweetAlertDialog.dismiss();
                                                RequestParams ps = new RequestParams();
                                                ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                                                ps.add("type", "2");
                                                ps.add("id", list.get(position - 1).getBid());
                                                UserClient.get(HttpUrl.DelBleEquip, ps, new AsyncHttpResponseHandler() {
                                                    @Override
                                                    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                                        String content = new String(response);
                                                        RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                                        if (ret.getCode().equals("200")) {
                                                            BleEquip obj = (BleEquip)list.get(position - 1);
                                                            if(null !=obj.getCategory() && "1".equals(obj.getCategory())) {
                                                                SharedPreferencesUtils.setParam(con, "xtyval", "");
                                                            }else{
                                                                SharedPreferencesUtils.setParam(con, "xyjval", "");
                                                            }
                                                            MyToastUtil.ShowToast(con, "成功");
                                                        }else{
                                                            MyToastUtil.ShowToast(con, ret.getInfo());
                                                        }
                                                        getBleEquips();
                                                    }

                                                    @Override
                                                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                                                    }
                                                });
                                            }
                                        })
                                        .show();
                            }
                        }
                    });
                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        getBleEquips();
    }
}
